/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;

import com.bayu.common.lib.CommonUtil;
import com.bayu.controller.MainController;
import com.bayu.services.dao.TIncomingUsageDao;
import com.bayu.services.entity.TIncomingUsage;
import java.awt.Color;
import java.awt.Shape;
import java.io.OutputStream;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author submonkey
 */
@Controller
public class SSLineGraph {

    private TIncomingUsageDao dao;
    private String tgl1;
    private String tgl2;

    @Autowired
    public void setDao(TIncomingUsageDao dao) {
        this.dao = dao;
    }

    @RequestMapping("/ss-linegraph.png")
    public void drawChart(OutputStream stream) throws Exception {
        JFreeChart chart = getChart();
        ChartUtilities.writeChartAsPNG(stream, chart, 800, 600);
    }

    @RequestMapping("/ss-linegraph-big.png")
    public void drawChartZoom(OutputStream stream) throws Exception {
        JFreeChart chart = getChart();
        ChartUtilities.writeChartAsPNG(stream, chart, 1024, 800);
    }

    private JFreeChart getChart() {
         XYDataset dataset =  getDatasetXY();

        String title = "STEEL SCRAP - Stock & Cummulative Incoming/Usage";
        if(!MainController.startDate.equals("")){
          tgl1 = CommonUtil.formatStringDate(MainController.startDate);
          tgl2 = CommonUtil.formatStringDate(MainController.endDate);
          title = title+"\r\n"+ tgl1+"-"+tgl2;
        }
        final JFreeChart chart = ChartFactory.createXYLineChart(
            title,
            "Date",
            "M/T",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
        chart.setBackgroundPaint(new Color(204, 255, 204));
       
        XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
    
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        
     
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        //renderer.setSeriesLinesVisible(0, false);
        //renderer.setSeriesShapesVisible(1, false);
        renderer.setSeriesPaint(0, new Color(224,207,17));
        renderer.setSeriesPaint(1, Color.BLUE);
        renderer.setSeriesPaint(2, Color.RED);
        renderer.setSeriesPaint(3, new Color(4,133,225));
        renderer.setSeriesPaint(4, new Color(209,71,11));
        renderer.setSeriesPaint(5, new Color(0,152,0));

        
     
        plot.setRenderer(renderer);

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        
     
        return chart;
    }

   

    private XYDataset getDatasetXY() {
        XYSeriesCollection dataset = new XYSeriesCollection();

        final XYSeries series1 = new XYSeries("Plan Cum Incoming");
        final XYSeries series2 = new XYSeries("Cum Incoming");
        final XYSeries series3 = new XYSeries("Plan Cum Usage");
        final XYSeries series4 = new XYSeries("Cum Usage");
        final XYSeries series5 = new XYSeries("Plan Stock");
        final XYSeries series6 = new XYSeries("Stock Actual");

        List<TIncomingUsage> data = this.dao.getAllByDate(MainController.startDate, MainController.endDate);
        double planCumIncoming = 0, cumIncoming = 0, planCumUsage = 0, cumUsage = 0;
        double planStock = data.get(0).getIdStock().getPlanStock();

        double stockActual = data.get(0).getIdStock().getStockActual();
        for (TIncomingUsage object : data) {
            String date[] = object.getTanggal().toString().split("-");
            double planIncoming = object.getPlanIncoming();
            double incoming = object.getIncoming();
            double planUsage = object.getPlanUsage();
            double usage = object.getUsageData();

            planCumIncoming += planIncoming;
            cumIncoming += incoming;
            planCumUsage += planUsage;
            cumUsage += usage;
            planStock += planIncoming - planUsage;
            stockActual += incoming - usage;

            //if(planIncoming !=0){
                series1.add(Double.parseDouble(date[2]),planCumIncoming);
            //}
            
            //dataset.addValue(planCumIncoming, "Plan Cum Incoming", date[2]);
            //if(incoming !=0){
                series2.add(Double.parseDouble(date[2]),cumIncoming);
            //}

            //if(planUsage !=0){
                series3.add(Double.parseDouble(date[2]),planCumUsage);
            //}
            
            if(usage!=0){
                series4.add(Double.parseDouble(date[2]),cumUsage);
            }
            series5.add(Double.parseDouble(date[2]),planStock);

            if(incoming==0 && usage==0){
                
            }else{
                series6.add(Double.parseDouble(date[2]),stockActual);
            }
            
        }
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        dataset.addSeries(series3);
        dataset.addSeries(series4);
        dataset.addSeries(series5);
        dataset.addSeries(series6);
        return dataset;
    }


}
